package fragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import java.util.ArrayList;
import java.util.List;

import adapter.ShiYuanGouFragmentAdapter;
import yztc.com.shiyuanbaoyou.R;

/**
 * Created by My on 2016/9/22.
 */
public class ShiYuanGouFragment extends Fragment {
    private TabLayout tabLayout;
    private String[] titles={"精选","10元","20元","30元"};
    private List<Fragment> fragments;
    private ViewPager viewPager;
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view=inflater.inflate(R.layout.fragment_shiyuangou,null);
        tabLayout= (TabLayout) view.findViewById(R.id.shiyuangou_tab);
        viewPager= (ViewPager) view.findViewById(R.id.vp_shiyuangou);
        initData();
        return view;
    }

    private void initData() {
        for (int i=0;i<titles.length;i++){
            tabLayout.addTab(tabLayout.newTab().setText(titles[i]));
        }
        viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
        tabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                viewPager.setCurrentItem(tab.getPosition());

            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {

            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });

        fragments=new ArrayList<>();
        JingXuanFragment f1=new JingXuanFragment();
        ShiYuanFragment f2=new ShiYuanFragment();
        ErShiYuanFragment f3=new ErShiYuanFragment();
        SanShiYuanFragment f4=new SanShiYuanFragment();

        fragments.add(f1);
        fragments.add(f2);
        fragments.add(f3);
        fragments.add(f4);

        ShiYuanGouFragmentAdapter adapter=new ShiYuanGouFragmentAdapter(getFragmentManager(),fragments);
        viewPager.setAdapter(adapter);
    }
}
